package erlwitch.compiler;

public class CompilerLine {
	private static final String WARNING_LABEL = "Warning: ";
	private String text;
	private int lineNumber;
	private String sourceFile;
	private boolean warning;
	private String message;
	
	public CompilerLine(String text) {
		this.text = text;
		String[] words = text.split(" ", 2);
		lineNumber = -1;
		message = text;
		sourceFile = "(unknown)";
		warning = false;

		if (words.length < 2) return;
		words[0] = words[0].substring(0, words[0].length() - 1);
		int colonIndex = words[0].lastIndexOf(':');
		if (colonIndex == -1) return;
		lineNumber = new Integer(words[0].substring(colonIndex + 1));
		sourceFile = words[0].substring(0, colonIndex);

		message = words[1];
		if (message.startsWith(WARNING_LABEL)) {
			warning = true;
			message = message.substring(WARNING_LABEL.length());
		}
	}
	
	public CompilerLine(boolean warning, String message) {
		this.warning = warning;
		this.message = message;
		this.text = message;
		this.sourceFile = "(unknown)";
		this.lineNumber = -1;
	}

	public String getText() {
		return text;
	}
	public void setText(String text) {
		this.text = text;
	}
	public int getLineNumber() {
		if (lineNumber < 1) return 1;
		return lineNumber;
	}
	public void setLineNumber(int lineNumber) {
		this.lineNumber = lineNumber;
	}
	public String getSourceFile() {
		return sourceFile;
	}
	public void setSourceFile(String sourceFile) {
		this.sourceFile = sourceFile;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}

	public boolean isWarning() {
		return warning;
	}

	public void setWarning(boolean warning) {
		this.warning = warning;
	}
	
	public String toString() {
		return text;
	}
}
